﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BIZ;
using DAL;

namespace SinhNhat.admin
{
    public partial class AdminParameter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            ParameterBIZ paramBIZ = new ParameterBIZ();
            List<PARAMETER> list = paramBIZ.GetAll();
            
            grParam.DataSource = list;
            grParam.DataBind();

            up.Update();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            grParam.RowEditing += new GridViewEditEventHandler(grParam_RowEditing);
            grParam.RowUpdating += new GridViewUpdateEventHandler(grParam_RowUpdating);
            grParam.RowCancelingEdit += new GridViewCancelEditEventHandler(grParam_RowCancelingEdit);
        }

        void grParam_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grParam.EditIndex = -1;
            LoadData();
        }

        void grParam_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtValue = (TextBox)grParam.Rows[e.RowIndex].Cells[2].FindControl("txtValue");

            PARAMETER param = new PARAMETER();
            param.ParameterID = new Guid(grParam.DataKeys[e.RowIndex].Value.ToString());
            param.Value = txtValue.Text.Trim();

            ParameterBIZ paramBIZ = new ParameterBIZ();
            paramBIZ.Edit(param);

            grParam.EditIndex = -1;
            LoadData();
        }

        void grParam_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grParam.EditIndex = e.NewEditIndex;
            LoadData();
        }
    }
}